﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fluq
{
	sealed class AutoDependency : IDependency, IDependencyLifestyle
	{
		private IDependencyKey _key;
		private Func<IDependencyContainer, IEnumerable<IDependencyParameter>, object> _factory;

		public AutoDependency(IDependencyKey key, Func<IDependencyContainer, IEnumerable<IDependencyParameter>, object> factory)
		{
			_key = key;
			_factory = factory;
		}

		public IDependencyKey Key { get { return _key; } }

		public Func<IDependencyContainer, IEnumerable<IDependencyParameter>, object> Factory { get { return _factory; } }

		public IDependencyLifestyle Lifestyle { get { return this; } }

		public object Acquire(object graphId, IDependencyKey key, Func<object> factory)
		{
			return factory();
		}

		public void Discard(IDependencyKey key, Action<object> disposer, object instance)
		{
			disposer(instance);
		}

		public Action<object> Disposer { get { return instance => { }; } }

		public IEnumerable<IDependencyParameter> Parameters { get { yield break; } }
	}
}
